
#include "at32f403a_407.h"
#include "cbb_init.h"
#include "cbb_delay.h"

#include "FreeRTOS.h"
#include "led.h"
#include "task.h"
// #include "stdio.h"

#include "myboard.h"
#include "device.h"
#include "uart.h"
#include "button.h"
#include "cbb_ph_ymodem_slave.h"
#include "cbb_verify_crc16.h"
#include <stdint.h>
#include "bsp_flash.h"

#include "upgrade_double_flash.h"

static PH_t* pstYMSlave;
static uint8_t u8RxData[1032];
static uint8_t u8TxData[4];

static uint8_t uart_ymodem_task(uint32_t u32RxLen){

    if(u32RxLen == 0) return 1;

    // // 打印数据
    // printf("recv: %ld\r\n", u32RxLen);

    // for(uint32_t i = 0; i < u32RxLen; i++){
    //     printf("%02x ", u8RxData[i]);
    // }
    // printf("\r\n");

    // 解析数据
    PH_Buf_t stRxBuffer = {
        .pu8Buffer = u8RxData,
        .u32BufferLen = sizeof(u8RxData),
        .u32DataLen = u32RxLen,
    };
    uint8_t u8Ret = cbb_ym_slave_parse(pstYMSlave, &stRxBuffer);

    // 解析失败，跳过
    if(u8Ret != PH_OK) return 1;
    
    // 构建数据
    PH_Buf_t stTxBuffer = {
        .pu8Buffer = u8TxData,
        .u32BufferLen = sizeof(u8TxData),
        .u32DataLen = 0,
    };
    u8Ret = cbb_ym_slave_build(pstYMSlave, &stTxBuffer);
    
    // 构建失败，跳过
    if(u8Ret != PH_OK) return 1;

    // 发送数据
    return uart_transmit(pstUsart1, stTxBuffer.pu8Buffer, stTxBuffer.u32DataLen);
}

static void start_ymodem_task(void){
    uint8_t u8Buf[4];
    PH_Buf_t stTxBuffer = {
        .pu8Buffer = u8Buf,
        .u32BufferLen = sizeof(u8Buf),
        .u32DataLen = 0,
    };
    
    cbb_ym_slave_start(pstYMSlave, &stTxBuffer);

    uart_transmit(pstUsart1, stTxBuffer.pu8Buffer, stTxBuffer.u32DataLen);
}

// void ymodem_err(void){
//     printf("ymodem err\r\n");
// }

// void ymodem_end(void){
//     printf("ymodem err\r\n");
// }

// void ymodem_data(uint8_t* pu8Data, uint32_t u32Len){
//     for(uint32_t i = 0; i < u32Len; i++){
//         printf("%02x ", pu8Data[i]);
//     }
//     printf("\r\n");
// }


int main(void)
{
    
    // 启动延时
    CBB_DELAY_MS(200);

    // 系统初始化
    cbb_system_init();

    PH_YM_Slave_t stYMSlave = {
        .u8Status = YM_STATUS_IDLE,
        .u16Tick = 0,
        .u16Timeout = 300,
        .u8Retry = 0,
        .u8Run = YM_IDLE,

        // .pfnEnd = ymodem_end,
        // .pfnErr = ymodem_err,
        // .pfnData = ymodem_data,
    };

    pstYMSlave = cbb_ym_slave_register(&stYMSlave, 
        cbb_verify_crc16_check, cbb_verify_crc16_append);


    UpgradeApp_t stUpgradeApp = {
        .HW = {
            .pfnRestart = NVIC_SystemReset,
            .pfnInfoRead = bsp_flash_read_byte,
            .pfnInfoWrite = bsp_flash_write
        }
    };
    upgrade_app_init(&stUpgradeApp);

    // 初始化延时
    CBB_DELAY_MS(200);

    // 内存剩余查询
    // printf("MEM Left: %ld B\r\n", cbb_get_s_mem_left());

    // 开启任务调度
    // vTaskStartScheduler();

    while(1)
    {

        // uart_monitor(pstUsart1);

        // if(get_device_event(pstUsart1, UART_DATA_READY)){
        //     clear_device_event(pstUsart1, UART_DATA_READY);
        //     uint32_t u32RxLen = uart_receive(pstUsart1,u8RxData);
        //     // 处理ymodem
        //     uart_ymodem_task(u32RxLen);
        // }


        // button_monitor(pstBtnMode);
        // if(get_device_event(pstBtnMode, BUTTON_CLICKED)){
        //     clear_device_event(pstBtnMode, BUTTON_CLICKED);
        
        //     led_on(pstLedBlue);
        //     upgrade_app_run();
        // }

        // led_toggle(pstLedGreen);

        button_monitor(pstBtnUpgrade);
        if(get_device_event(pstBtnUpgrade, BUTTON_CLICKED)){
            clear_device_event(pstBtnUpgrade, BUTTON_CLICKED);
            
            led_on(pstLedGreen);
            upgrade_app_run();
        }
        led_toggle(pstLedBlue);

        CBB_DELAY_MS(2000);

    }

}
